package pri.black.garden.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Objects;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pri.black.dogview.domain.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import pri.black.garden.domain.Block;
import pri.black.garden.service.IBlockService;
import pri.black.garden.biz.BlockBiz;

/**
 * gardenController
 * 
 * @author black
 * @date 2025-01-21
 */
@RestController
@RequestMapping("/garden/block")
public class BlockController
{
    @Autowired
    private BlockBiz blockBiz;

    /**
     * 查询garden列表
     */
    //@PreAuthorize("@ss.hasPermi('garden:garden:list')")
    @GetMapping("/list")
    public R list()
    {
        List<Block> list = blockBiz.getService().list();
        for (Block block : list) {
            if (block.getPid() == null) {
                block.setPid(0L);
            }
        }
        return R.ok(list);
    }

    /**
     * 获取garden详细信息
     */
    //@PreAuthorize("@ss.hasPermi('garden:garden:query')")
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") Long id)
    {
        return R.ok(blockBiz.getService().getById(id));
    }

    /**
     * 新增garden
     */
    //@PreAuthorize("@ss.hasPermi('garden:garden:add')")
    //@Log(title = "garden", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody Block block)
    {
        return R.ok(blockBiz.save(block));
    }

    /**
     * 修改garden
     */
    //@PreAuthorize("@ss.hasPermi('garden:garden:edit')")
    //@Log(title = "garden", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody Block block)
    {
        return R.ok(blockBiz.update(block));
    }

    /**
     * 删除garden
     */
    //@PreAuthorize("@ss.hasPermi('garden:garden:remove')")
    //@Log(title = "garden", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids)
    {
        return R.ok(blockBiz.getService().removeByIds(Arrays.asList(ids)));
    }

    @GetMapping("block-area")
    public R blockArea(){
        List<Block> list = blockBiz.getService().lambdaQuery().isNull(Block::getPid).list();
        for (Block block : list) {
            block.setChildren(blockBiz.getService().lambdaQuery().eq(Block::getPid, block.getId()).list());
        }
        return R.ok(list);
    }

    @GetMapping("block-first")
    public R firstBlock(){
        return R.ok(blockBiz.getService().lambdaQuery().isNull(Block::getPid).list());
    }


    @PostMapping("move")
    public R moveOrder(@RequestBody Block block) {

        Block blockCache = blockBiz.getService().getById(block.getId());
        Long pid = blockCache.getPid();
        List<Block> list;
        if (pid == null) {
            list = blockBiz.getService()
                    .lambdaQuery()
                    .isNull(Block::getPid)
                    .orderByAsc(Block::getListOrder)
                    .list();
        }else {
            list = blockBiz.getService()
                    .lambdaQuery()
                    .eq(Block::getPid, pid)
                    .orderByAsc(Block::getListOrder)
                    .list();
        }

        if (block.getListOrder() == 1) {


            for (int i = 0; i < list.size(); i++) {
                Block blk = list.get(i);
                blk.setListOrder(i);
                if (Objects.equals(blk.getId(), blockCache.getId())) {
                    if (i == list.size() - 1) {
                        continue;
                    }else {
                        Block next = list.get(i + 1);
                        next.setListOrder(i);
                        blk.setListOrder(i + 1);
                        i++;
                    }
                }
            }

            for (int i = 0; i < list.size(); i++) {
                Block blk = list.get(i);
                Block update = new Block();
                update.setId(blk.getId());
                update.setListOrder(blk.getListOrder());
                blockBiz.getService().updateById(update);
            }

        } else if (block.getListOrder() == -1) {

            for (int i = 0; i < list.size(); i++) {
                Block blk = list.get(i);
                blk.setListOrder(i);
                if (Objects.equals(blk.getId(), blockCache.getId())) {
                    if (i == 0) {
                        continue;
                    }else {
                        Block pre = list.get(i - 1);
                        pre.setListOrder(i);
                        blk.setListOrder(i - 1);
                        i++;
                    }
                }
            }

            for (int i = 0; i < list.size(); i++) {
                Block blk = list.get(i);
                Block update = new Block();
                update.setId(blk.getId());
                update.setListOrder(blk.getListOrder());
                blockBiz.getService().updateById(update);
            }
        }else {
            return R.fail();
        }
        return R.ok();
    }

}
